using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Firestarter.DB.Schema.BO;
using Firestarter.DB_DomainModel.Logic;
using FireStarterModeller.BO;
using Firestarter.DB.Schema.Logic;
using FireStarterModeller.Logic;
using FireStarterModeller.Test;
using FireStarterModeller.UI;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace Firestarter.DB.Schema.UI
{
    public class ViewTestDbRevEngWizard
    {
        private IWizardControl _wizardControl;
        private IWizardForm _wizardForm;

        public ViewTestDbRevEngWizard()
        {
            ReverseEngineering reverseEngineering = CreateTestReverseEngineering();
            DBToDMImporterFactory dbToDMImporterFactory = new DBToDMImporterFactory(new DBTypeToDMPropertyTypeMapper(new PropertyTypeStorer(reverseEngineering.GetSolution())), new RelationshipNameGenerator());
            IDBChangesImporter dbChangesImporter = dbToDMImporterFactory.CreateDBChangesImporter();
            IControlFactory factory = GlobalUIRegistry.ControlFactory;
            IFireStarterFormsController fireStarterFormsController = new FireStarterFormsController(factory);
            DbSchemaReverseEngineerWizardController wizardController = new DbSchemaReverseEngineerWizardController(fireStarterFormsController, reverseEngineering, dbChangesImporter);
            _wizardForm = factory.CreateWizardForm(wizardController);
            _wizardForm.Size = new Size(800, 600);
            _wizardControl = _wizardForm.WizardControl;
            _wizardControl.MessagePosted += (message => MessageBox.Show(message));
            //BorderLayoutManager layoutManager = factory.CreateBorderLayoutManager(this);
            //layoutManager.AddControl(_wizardControl, BorderLayoutManager.Position.Centre);
        }

        public void ShowWizardForm()
        {
            _wizardForm.Show();
            //_wizardControl.Start();
        }

        private ReverseEngineering CreateTestReverseEngineering()
        {
            ReverseEngineering reverseEngineering = new ReverseEngineering();
            reverseEngineering.StoredDatabase = CreateTestDatabaseInfo();
            reverseEngineering.DatabaseConnectionInfo = CreateTestDatabaseConnection();
            reverseEngineering.ReverseEngineeringSettings = new ReverseEngineeringSettings();
            return reverseEngineering;
        }

        private DatabaseConnectionInfo CreateTestDatabaseConnection()
        {
            DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo
            {
                Vendor = "MySql",
                Server = "localhost",
                Database = "SchemaInfo_Test_Fixed",
                UserName = "root",
                Password = "root"
            };
            return databaseConnectionInfo;
        }

        public static DBDatabase CreateTestDatabaseInfo()
        {
            //DBDatabase dbDatabase = new DBDatabase { DatabaseName = "MyDatabase", PendingChangeType = ChangeType.Update };
            //Table tableInfo = new Table("MyTable") { PendingChangeType = ChangeType.Update };
            //dbDatabase.Tables.Add(tableInfo);
            //Column columnInfoID = new Column("MyTableID");
            //tableInfo.Columns.Add(columnInfoID);
            //Column columnInfo = new Column("MyColumn") { PendingChangeType = ChangeType.Update };
            //tableInfo.Columns.Add(columnInfo);
            //tableInfo.Columns.Add(new Column("MyColumn3") { PendingChangeType = ChangeType.Add });
            //tableInfo.Columns.Add(new Column("MyColumn2") { PendingChangeType = ChangeType.Delete });
            //DBKey keyInfo = new DBKey() { KeyName = "MyKey" };
            //tableInfo.Keys.Add(keyInfo);
            //keyInfo.KeyColumns.Add(new DBKeyColumn() { Column = columnInfo });
            //tableInfo.PrimaryKey = new DBPrimaryKey();
            //tableInfo.PrimaryKey.PrimaryKeyColumns.Add(new DBPrimaryKeyColumn { Column = columnInfoID });

            //Table tableInfo2 = new Table("MyUpdatedTable") { PendingChangeType = ChangeType.Update };
            //dbDatabase.Tables.Add(tableInfo2);
            //Column table2ColumnID = new Column("MyTable2ID");
            //tableInfo2.Columns.Add(table2ColumnID);
            //Column table2ColumnInfo = new Column("UpdatedColumn") { PendingChangeType = ChangeType.Update };
            //tableInfo2.Columns.Add(table2ColumnInfo);
            //Column table2AddedColumn = new Column("AddedColumn") { PendingChangeType = ChangeType.Add };
            //tableInfo2.Columns.Add(table2AddedColumn);
            //tableInfo2.Columns.Add(new Column("DeletedColumn") { PendingChangeType = ChangeType.Delete });
            //Column table2UnchangedColumn = new Column("UnchangedColumn") { PendingChangeType = ChangeType.None };
            //tableInfo2.Columns.Add(table2UnchangedColumn);
            //DBKey table2KeyInfo = new DBKey() { KeyName = "UnchangedKey" };
            //tableInfo2.Keys.Add(table2KeyInfo);
            //table2KeyInfo.KeyColumns.Add(new DBKeyColumn() { Column = table2UnchangedColumn });
            //DBKey table2AddedKeyInfo = new DBKey() { KeyName = "AddedKey", PendingChangeType = ChangeType.Add };
            //tableInfo2.Keys.Add(table2AddedKeyInfo);
            //table2AddedKeyInfo.KeyColumns.Add(new DBKeyColumn() { Column = table2AddedColumn, PendingChangeType = ChangeType.Add });
            //tableInfo2.PrimaryKey = new DBPrimaryKey();
            //tableInfo2.PrimaryKey.PrimaryKeyColumns.Add(new DBPrimaryKeyColumn { Column = table2ColumnID, PendingChangeType = ChangeType.Delete });
            //tableInfo2.PrimaryKey.PrimaryKeyColumns.Add(new DBPrimaryKeyColumn { Column = table2UnchangedColumn, PendingChangeType = ChangeType.Add });

            DBTableBuilder dbTableBuilder = new DBTableBuilder(new DBTable("ParentTable"));
            dbTableBuilder.AddPrimaryKeyColumn();
            dbTableBuilder.AddColumn("ParentName");
            DBTableBuilder childDBTableBuilder = new DBTableBuilder(new DBTable("ChildTable"));
            childDBTableBuilder.AddPrimaryKeyColumn();
            //childDBTableBuilder.AddColumn("ChildName");
            childDBTableBuilder.AddForeignKey("ForeignKeyName", dbTableBuilder.Table);
            DBDatabase dbDatabase = new DBDatabase();
            dbDatabase.Tables.Add(dbTableBuilder.Table);
            dbDatabase.Tables.Add(childDBTableBuilder.Table);

            dbTableBuilder.Table.TableName = dbTableBuilder.Table.TableName.ToLower();
            childDBTableBuilder.Table.TableName = childDBTableBuilder.Table.TableName.ToLower();

            return dbDatabase;
        }

        #region Implementation of IFormControl

        /// <summary>
        ///             Sets the form to control
        /// </summary>
        /// <param name="form">The form to control</param>
        public void SetForm(IFormHabanero form)
        {
            
        }

        #endregion
    }
}
